package com.bsy.videostation.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bsy.common.decoration.GridDividerItemDecoration
import com.bsy.common.utils.dp2px
import com.bsy.framework.base.BaseMvvmFragment
import com.bsy.framework.helper.AppHelper
import com.bsy.videostation.adapter.AllMainAdapter
import com.bsy.videostation.ui.activity.WebViewActivity
import com.bsy.videostation.viewmodel.MainViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.dg.videostation.databinding.FragmentAllBinding
import me.jessyan.autosize.AutoSize

/**
 * Created by Lucy on 2024/2/21.
 * Copyright
 * 描述 :Category
 */
class AllFragment : BaseMvvmFragment<FragmentAllBinding, MainViewModel>(), OnItemClickListener {
    private val mAdapter: AllMainAdapter by lazy {
        AllMainAdapter()
    }

    //是否打开新页面
    private var isOpenNewPage = false
    override fun initViewModel() {
        mViewModel = AppHelper.getApplicationViewModel(MainViewModel::class.java)
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        initRv()
    }

    override fun initData() {
//        mViewModel.getAppInfoList()
    }


    private fun initRv() {
        mBinding?.rvContent?.apply {
            this.layoutManager = GridLayoutManager(context, 3, RecyclerView.HORIZONTAL, false)
            addItemDecoration(
                GridDividerItemDecoration(dp2px(20F), false)
            )
            adapter = mAdapter
        }
    }

    override fun initListener() {
        mAdapter.setOnItemClickListener(this)
    }

    override fun initObserver() {
        mViewModel.appData.observe(viewLifecycleOwner) {
            if (mBinding?.rvContent?.adapter == null)
                mBinding?.rvContent?.adapter = mAdapter
            mAdapter.setList(it)
        }
        mViewModel.allScrollToFirstPosition.observe(viewLifecycleOwner) {
            if (!isOpenNewPage && it == 0)
                mBinding?.rvContent?.scrollToPosition(0)
            else
                isOpenNewPage = false
        }
    }

    companion object {
        fun newInstance() = AllFragment()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        AutoSize.autoConvertDensity(activity, 768F, false)
        return getContentView(inflater, container)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
//        mViewModel.allEvent.value = AllViewType.VIDEO_LIST_PAGE.value
        val item = this.mAdapter.getItem(position)
        if (item.detail.isNullOrEmpty()) {
            showToast("No play address found")
            return
        }
        //是否打开新页面
        isOpenNewPage = true
        val intent = Intent(context ?: requireContext(), WebViewActivity::class.java)
        val bundle = Bundle().apply {
            putString("url", item.detail)
        }
        intent.putExtras(bundle)
        startActivity(intent)
    }

}